﻿using Mozhi.Abp.Core.Extensions;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using Volo.Abp;
using Volo.Abp.Domain.Values;

namespace Mozhi.Abp.Domain.BasicData
{
    public class PaymentTerm : ValueObject
    {
        [JsonProperty]
        public Guid Id { get; private set; }

        [JsonProperty]
        public int BasisType { get; private set; }

        [JsonProperty]
        public int Days { get; private set; }

        [JsonProperty]
        public string Code {  get; private set; }

        [JsonProperty]
        public string Name { get; private set; }

        [JsonProperty]
        public bool Activated {  get; private set; }

        [JsonProperty]
        public int Type { get; private set; }


        public void Validate()
        {
            if (!Activated)
            {
                var typeName = GetTypeName();

                throw new UserFriendlyException($"{typeName}:{Code}-{Name}已禁用", "100000");
            }  
        }

        public string GetTypeName()
        {
            if (Type == 1)
                return "付款条件";

            if (Type == 2)
                return "收款条件";

            return "";
        }

        public DateTime CalculateDate(DateTime date)
        {
            //交易日后天数
            if (Type == 1)
                return date.AddDays(Days);

            //交易日下一个月天数
            if(Type == 2)
            {
                var nextMonthDate = date.AddMonths(1);

                return nextMonthDate.AddDays(Days);
            }

            //下一个月
            if(Type==3)
            {
                var nextMonthDate = date.AddMonths(1).ToFirstDayDateTime();

                return nextMonthDate.AddDays(Days);
            }

            //当前月天数
            if (Type == 4)
            {
                var nextMonthDate = date.ToFirstDayDateTime();

                return nextMonthDate.AddDays(Days);
            }

            throw new ArgumentNullException(nameof(Type));
        }


        protected override IEnumerable<object> GetAtomicValues()
        {
            throw new NotImplementedException();
        }
    }
}
